package com.train.${module}.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.${module}.resp.${Domain}PageResp;
import com.train.common.exception.GlobalException;
import com.train.common.enums.impl.BizExceptionEnums;
import com.train.common.resp.PageResp;
import com.train.${module}.dto.${Domain}AddOrEditDTO;
import com.train.${module}.dto.${Domain}PageDTO;
import com.train.${module}.mapper.${Domain}Mapper;
import com.train.${module}.po.${Domain}PO;
import com.train.${module}.service.${Domain}Service;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author ${author}
 * @description 针对表【${domain}(${tableNameCn})】的数据库操作Service实现
 * @createDate ${date}
 */
@Service
@RequiredArgsConstructor
public class ${Domain}ServiceImpl extends ServiceImpl<${Domain}Mapper, ${Domain}PO>
    implements ${Domain}Service {

    private final ${Domain}Mapper ${domain}Mapper;

    @Override
    public void addOrEdit${Domain}(${Domain}AddOrEditDTO ${domain}AddOrEditDTO) {
        //校验id是否存在,判断是insert还是update
        if (${domain}AddOrEditDTO.getId() != null) {
            //如果是编辑,需要校验id记录是否存在
            if (!${domain}Mapper.existsId(${domain}AddOrEditDTO.getId())) {
                throw new GlobalException(BizExceptionEnums.EXP_${DOMAIN}_NOT_EXISTS);
            }
        }

        ${Domain}PO ${domain}PO = BeanUtil.copyProperties(${domain}AddOrEditDTO, ${Domain}PO.class);
        boolean isSaved = this.saveOrUpdate(${domain}PO);
        if (!isSaved) {
            throw new GlobalException(BizExceptionEnums.EXP_ADD_${DOMAIN});
        }
    }

    @Override
    public PageResp<${Domain}PageResp> pageList(${Domain}PageDTO ${domain}PageDTO) {
        return ${domain}Mapper.pageList(${domain}PageDTO);
    }

    @Override
    public void remove(Long id) {
        boolean isRemoved = this.removeById(id);
        if (!isRemoved) {
            throw new GlobalException(BizExceptionEnums.EXP_REMOVE_${DOMAIN});
        }
    }
}




